<template>
	<view>
		<view class="inputBox">
			<u-input border="true" auto-height="true" :height="height" type="textarea" v-model="text" />
		</view>
		<view class="comment-list">
			<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple :maxCount="10">
			</u-upload>
		</view>

		<view class="submitBox">
			<u-button class="uButton" type="primary" @click="submit">提交</u-button>
		</view>
	</view>
</template>

<script>
	import {
		login
	} from "../../du-server/api.js"
	export default {
		components: {},
		data() {
			return {
				height: 500,
				fileList1: [],
			};
		},
		onLoad() {},
		onShow() {},
		methods: {
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1);
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file);
				let fileListLen = this[`fileList${event.name}`].length;
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: "uploading",
						message: "上传中",
					});
				});
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url);
					let item = this[`fileList${event.name}`][fileListLen];
					this[`fileList${event.name}`].splice(
						fileListLen,
						1,
						Object.assign(item, {
							status: "success",
							message: "",
							url: result,
						})
					);
					fileListLen++;
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: "http://192.168.2.21:7001/upload", // 仅为示例，非真实的接口地址
						filePath: url,
						name: "file",
						formData: {
							user: "test",
						},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data.data);
							}, 1000);
						},
					});
				});
			},
			async submit() {
				login({
					name: "yuyou123",
					password: "123456"
				})
				const result = await login({
					name: "yuyou123",
					password: "123456"
				})
				console.log(result)
			}
		},
	};
</script>

<style lang="scss">
	.inputBox {
		padding: 20rpx;
	}

	.submitBox {
		width: 750rpx;
		padding: 0 100rpx;
		position: fixed;
		bottom: 30rpx;
		z-index: 99;

		.uButton {
			width: 50%;
		}
	}

	.comment-list {
		// background-color: rgba($color: pink, $alpha: 0.3);
		padding: 40rpx;
	}

	// .comment-list .item {
	// 	padding-top: 30rpx;
	// 	padding-bottom: 30rpx;
	// 	border-top: none;
	// 	border-bottom: 1px solid #dddddd;
	// }

	.imgs {
		display: flex;
		flex-wrap: wrap;
		padding-top: 30rpx;
		padding-bottom: 30rpx;
		border-top: none;
		// border-bottom: 1px solid #dddddd;
	}

	.imgs .box {
		position: relative;
		width: 210rpx;
		height: 210rpx;
		margin-top: 10rpx;
		margin-right: 10rpx;
		border: 1px solid #dddddd;
	}

	.imgs .box:nth-child(3n) {
		margin-right: 0;
	}

	.imgs .box .imageList {
		width: 100%;
		height: 100%;
	}

	.cmt-user {
		.left {
			display: flex;

			image {
				width: 100rpx;
				height: 100rpx;
				border-radius: 50%;
			}
		}
	}

	.iconClass {
		position: absolute;
		top: 5rpx;
		right: 5rpx;
		z-index: 99;
		width: 50rpx;
		height: 50rpx;
		background-color: #ffffff;
		border-radius: 50%;
	}
</style>
